//
//  StockContext.m
//  Stock
//
//  Created by blacksun on 11-6-23.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StockContext.h"

#define kRadarDateKey     @"radarDate"

@implementation StockContext

@synthesize workingStock;
@synthesize sections;
@synthesize remoteMyStocks;
@synthesize isFavorite;
@synthesize userId;
@synthesize updateTimes;
@synthesize radarDays;
@synthesize radarTest;
@synthesize needDisplayOpnition;
@synthesize paid;


-(MyStock *)findRemoteStockWithStockId:(NSString *)stockId{
    if (!remoteMyStocks || [remoteMyStocks count]==0) {
        //NSLog(@"local");
        self.remoteMyStocks = [NSMutableArray arrayWithArray:[MyStock allObjects]];// 
    }
    //NSLog(@"find %@",stockId);
    for (MyStock *remoteStock in remoteMyStocks) {
        if ([remoteStock.uuid isEqualToString:stockId]) {
            return remoteStock;
        }
    }
    return nil;
}
-(int)findMaxIndexWithSectionId:(NSString *)sectionId{
    int maxIndex = 0;
    if (!sections || [sections count]==0) {
        NSLog(@"query");
        self.sections = [Section allObjects];
    }
    for (Section *section in sections) {
        if ([sectionId isEqualToString:section.uuid]) {
            maxIndex = section.maxIndex;
            break;
        }
    }
    return maxIndex;
}

-(RadarServiceStatu)radarStatus{
    //return RadarServiceStatuWarning;
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    BOOL didFinishQuestions = [userDefault boolForKey:@"finisheQuestions"];
    /*if(self.paid && self.radarDays>10){
        return RadarServiceStatuNormal;
    }else */if(self.paid && self.radarDays>=0/* && self.radarDays<10*/){
        return RadarServiceStatuWarning;
    }else if (self.radarTest && didFinishQuestions) {
        return RadarServiceStatuTrialAndFinishQuestion;
    }else if (self.radarTest) {
        return RadarServiceStatuTrial;
    }else if(!self.radarTest && !self.paid && self.radarDays>0){
        return RadarServiceStatuWarning;
    }else if(self.radarDays<=0){
        return RadarServiceStatuExpire;
    }else{
        return RadarServiceStatuNormal;
    }
}
-(void)setFinishQuestions{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setBool:YES  forKey:@"finisheQuestions"];
    [userDefault synchronize];
}

-(void)updateaRadarDate:(NSString*)date{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:date forKey:kRadarDateKey];
    [userDefault synchronize];
}
-(NSString *)getRadaDate{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    return [userDefault objectForKey:kRadarDateKey];
}
-(void)saveChargeEmail:(NSString *)email{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:email forKey:@"ChargeEmail"];
    [userDefault synchronize];
}
-(void)saveChargePassword:(NSString *)password{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:password forKey:@"chargePassword"];
    [userDefault synchronize];
}
-(NSString *)savedChargeEmail{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    return [userDefault objectForKey:@"ChargeEmail"];
}

//单例实现
static StockContext *instance = nil;
+(StockContext *)shareInstance{
    @synchronized(self){
        if (instance == nil) {
            instance = [[self alloc] init];
        }
    }
    return instance;
}
+ (id)allocWithZone:(NSZone *)zone{
    @synchronized(self) {
        if (instance == nil) {
            instance = [super allocWithZone:zone];
            return instance;  // assignment and return on first allocation
        }
    }
    
    return nil; //on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone{
    return self;
}

- (id)retain{
    return self;
}

- (unsigned)retainCount{
    return UINT_MAX;  //denotes an object that cannot be released
}

- (void)release{
    //do nothing
}

- (id)autorelease{
    return self;
}
@end
